﻿package net.wangit.dev.settings;

import net.wangit.MapList;
import net.wangit.Path;
import net.wangit.context.ActionContext;
import net.wangit.framework.config.WizardSettings;
import net.wangit.support.Action;
import net.wangit.support.MapListFactory;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class WizardSettingsHelper implements MapListFactory, Action {

	public WizardSettingsHelper() {
	}

	@Override
	public ActionContext execute(ActionContext ac) throws Exception {
		this.saveToXML(ac);
		WizardSettings.instance().load();
		return ac;
	}

	private void saveToXML(ActionContext ac) throws Exception {
		String path = Path.getConfigPath(WizardSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			FileUtil.delete(path);
		}
		Document doc = DocumentHelper.createDocument();
		Element el = doc.addElement("wizard");
		el.addElement("formColumn").setText(
				ac.getRequestParameter("wizardsettings.attr11"));
		el.addElement("formColsConfig").setText(
				ac.getRequestParameter("wizardsettings.attr12"));
		el.addElement("formShowUnitBorder").setText(
				ac.getRequestParameter("wizardsettings.attr13"));
		el.addElement("formShowTitleBorder").setText(
				ac.getRequestParameter("wizardsettings.attr14"));
		el.addElement("formShowGridBorder").setText(
				ac.getRequestParameter("wizardsettings.attr15"));
		el.addElement("formShowCellBorder").setText(
				ac.getRequestParameter("wizardsettings.attr16"));

		el.addElement("listShowUnitBorder").setText(
				ac.getRequestParameter("wizardsettings.attr21"));
		el.addElement("listShowTitleBorder").setText(
				ac.getRequestParameter("wizardsettings.attr22"));
		el.addElement("listShowGridBorder").setText(
				ac.getRequestParameter("wizardsettings.attr23"));
		el.addElement("listShowCellBorder").setText(
				ac.getRequestParameter("wizardsettings.attr24"));

		el.addElement("queryColumn").setText(
				ac.getRequestParameter("wizardsettings.attr31"));
		el.addElement("queryColsConfig").setText(
				ac.getRequestParameter("wizardsettings.attr32"));
		el.addElement("queryShowUnitBorder").setText(
				ac.getRequestParameter("wizardsettings.attr33"));
		el.addElement("queryShowTitleBorder").setText(
				ac.getRequestParameter("wizardsettings.attr34"));
		el.addElement("queryShowGridBorder").setText(
				ac.getRequestParameter("wizardsettings.attr35"));
		el.addElement("queryShowCellBorder").setText(
				ac.getRequestParameter("wizardsettings.attr36"));
		XMLUtil.writeUTF8(doc, path);
	}

	@Override
	public MapList getMapList() {
		MapList list = new MapList();
		WizardSettings wiz = WizardSettings.instance();
		list.put(0, "attr11", wiz.getFormColumn());
		list.put(0, "attr12", wiz.getFormColsConfig());
		list.put(0, "attr13", wiz.getFormShowUnitBorder());
		list.put(0, "attr14", wiz.getFormShowTitleBorder());
		list.put(0, "attr15", wiz.getFormShowGridBorder());
		list.put(0, "attr16", wiz.getFormShowCellBorder());

		list.put(0, "attr21", wiz.getListShowUnitBorder());
		list.put(0, "attr22", wiz.getListShowTitleBorder());
		list.put(0, "attr23", wiz.getListShowGridBorder());
		list.put(0, "attr24", wiz.getListShowCellBorder());

		list.put(0, "attr31", wiz.getQueryColumn());
		list.put(0, "attr32", wiz.getQueryColsConfig());
		list.put(0, "attr33", wiz.getQueryShowUnitBorder());
		list.put(0, "attr34", wiz.getQueryShowTitleBorder());
		list.put(0, "attr35", wiz.getQueryShowGridBorder());
		list.put(0, "attr36", wiz.getQueryShowCellBorder());
		return list;
	}

	public ActionContext restore(ActionContext ac) throws Exception {
		String path = Path.getConfigPath(WizardSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			FileUtil.delete(path);
		}
		WizardSettings.instance().loadDefault();
		return ac;
	}
}
